package com.cheng.example.easypoi.charper01.j01;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.date.DateUtil;

import java.io.File;
import java.time.LocalTime;
import java.util.*;

/**
 * @author chengshuai
 * @version 1.0
 * @date 2020/6/20 9:51
 */
public class Client {

    public static void main(String[] args) {

        String year = null;
        String mouth = null;

        // 1、得到月份  todo 比较烂，以后待修正
        ImportParams params1 = new ImportParams();
        params1.setTitleRows(0);
        params1.setHeadRows(1);
        File file1 = new File("C:\\Users\\Medvalley\\Desktop\\work content\\015、考勤模块开发\\考勤统计.xlsx");
        List<Map<String, Object>> list1 = ExcelImportUtil.importExcel(file1, Map.class, params1);

        System.out.println("得到标题：" + list1.get(0).entrySet());
        for (Map.Entry entry : list1.get(0).entrySet()) {
            String key = (String) entry.getKey();
            //得到年
            year = key.substring(0, 4);
            //得到月
            mouth = key.substring(5, 7);
            System.out.println(year + "年" + mouth + "月");
            break;
        }
        // 2、得到个人打卡情况
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        params.setTitleRows(3);

        File file = new File("C:\\Users\\Medvalley\\Desktop\\work content\\015、考勤模块开发\\考勤统计.xlsx");
        List<Map<String, Object>> list = ExcelImportUtil.importExcel(file, Map.class, params);
        Map<String, Object> map = list.get(0);
        // 3、得到所有日期
        List<Integer> dayList = new ArrayList<>();
        map.entrySet().stream().forEach(e -> {
            if (e.getKey().contains("/")) {
                String key = e.getKey();
                String day = key.split("/")[0];
//                    System.out.println(day);
                dayList.add(Integer.valueOf(day));
            }
        });
        Collections.sort(dayList);

        List<SignPerson> signPersonList = new ArrayList<>();
        SignPerson signPerson;

        List<SingDayDetail> detailsListList = null;
        System.out.println(list.get(1));
        // 得到每个人的个人数据
        for (int i = 0; i < list.size(); i++) {
            map = list.get(i);
            if (i % 2 == 0) {

                signPerson = new SignPerson();
                detailsListList = new ArrayList<>();
                signPerson.setSingDayDetailList(detailsListList);
                signPerson.setName((String) map.get("姓名"));

                signPersonList.add(signPerson);
                for (Map.Entry e : map.entrySet()) {

                    for (Integer j : dayList) {
                        if (((String) e.getKey()).contains(String.valueOf(j))) {

                            // Excel导入
                            SingDayDetail singDayDetail = new SingDayDetail();
                            String value = (String) e.getValue();
                            singDayDetail.setSignDateNote((String) e.getKey());

                            // 日期
                            String dateStr = year + "-" + mouth + "-" + j;

                            // 设置日期
                            Date date = DateUtil.parse(dateStr);
                            singDayDetail.setSignDate(date);

                            if (value == null) {
                                continue;
                            }

                            if (value.contains("漏刷")) {
                                singDayDetail.setSignInNote("漏刷");
                                detailsListList.add(singDayDetail);
                                continue;
                            }

                            if (value.contains("迟到")) {
                                String[] values = value.split(" ");
                                String time = values[0];
                                String hours = time.split(":")[0];
                                String minute = time.split(":")[1];

                                // 创建时间
                                LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));
                                singDayDetail.setSignInNote("迟到");
                                singDayDetail.setSignInTime(localTime);
                                detailsListList.add(singDayDetail);
                                continue;
                            }

                            String hours = value.split(":")[0];
                            String minute = value.split(":")[1];

                            // 创建时间
                            LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));
                            singDayDetail.setSignInTime(localTime);
                            detailsListList.add(singDayDetail);
                        }
                    }
                }
            } else {
                for (Map.Entry e : map.entrySet()) {

                    for (SingDayDetail j : detailsListList) {
                        if (e.getKey().equals(j.getSignDateNote())) {
                            if (e.getValue() == null) {
                                continue;
                            }

                            String value = (String) e.getValue();
                            if (value.contains("漏刷")) {
                                j.setSignInNote("漏刷");
                                continue;
                            }

                            if (value.contains("早退")) {
                                j.setSignOffNote("早退");
                                String[] values = value.split(" ");
                                String time = values[0];
                                String hours = time.split(":")[0];
                                String minute = time.split(":")[1];
                                // 创建时间
                                LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));
                                j.setSignOffTime(localTime);
                                continue;
                            }

                            String hours = value.split(":")[0];
                            String minute = value.split(":")[1];
                            LocalTime localTime = LocalTime.of(Integer.valueOf(hours), Integer.valueOf(minute));

                            j.setSignOffTime(localTime);
                        }
                    }

                }

            }
        }

        System.out.println(signPersonList.size());
        for (SingDayDetail s : signPersonList.get(0).getSingDayDetailList()) {
            System.out.println(s);
        }


    }
}
